package com.mars.push.core.service;

import com.mars.fw.web.exception.KingException;
import com.mars.push.api.common.DeviceType;
import com.mars.push.api.common.MessageInput;
import com.mars.push.api.dto.PushMessageInput;
import com.mars.push.api.exception.PushException;
import com.mars.push.core.channel.PushChannelContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author King
 * @create 2020/7/10 14:50
 */
@Slf4j
@Service
public class PushServiceImpl implements IPushService {

    @Autowired
    private PushChannelContext pushChannelContext;

    @Override
    public Object pushAssign(MessageInput input) {
        return null;
    }

    @Override
    public Object push(MessageInput input) {
        try {
            PushMessageInput param = (PushMessageInput) input;
            if (param.getDeviceType().getCode() == DeviceType.ANDROID.getCode()) {
                return pushChannelContext.instance(((PushMessageInput) input).getPushType().getCode()).androidPush(input);
            } else if (param.getDeviceType().getCode() == DeviceType.IOS.getCode()) {
                return pushChannelContext.instance(((PushMessageInput) input).getPushType().getCode()).iosPush(input);
            } else {
                return pushChannelContext.instance(((PushMessageInput) input).getPushType().getCode()).allPush(input);
            }
        } catch (PushException e) {
            log.error(e.getMessage(), e);
            throw new KingException(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new KingException("推送服务内部异常");
        }
    }
}
